package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.FacadeWorkCellInstanceMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeFatherItem;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeStep;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeWorkCellInstance;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.FacadeWorkCellInstanceService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.FacadeWorkOrderService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomFileService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作内容service实现类
 * Created by yangcs on 2017/2/21.
 */
public class FacadeWorkCellInstanceServiceImpl implements FacadeWorkCellInstanceService {

    @Autowired
    private FacadeWorkCellInstanceMapper facadeWorkCellInstanceMapper;
    @Autowired
    private FacadeWorkOrderService facadeWorkOrderService;
    @Autowired
    private SomFileService fileService;

    /**
     * 分页查询工作内容列表
     *
     * @param category
     * @return
     */
    @Override
    public List<FacadeWorkCellInstance> queryByParams(String category) {
        Map map = new HashMap<>();
        map.put("category",category);
        return facadeWorkCellInstanceMapper.queryByParams(map);
    }

    @Override
    public List<String> queryCategorys(){
        return facadeWorkCellInstanceMapper.queryCategorys();
    }

    @Override
    public List<FacadeWorkCellInstance> queryByEntityId(String entityId) {
        return facadeWorkCellInstanceMapper.queryByEntityId(entityId);
    }

    /**
     * 根据id查询详情
     *
     * @param workInstanceId
     * @return
     */
    @Override
    public FacadeWorkCellInstance getById(String workInstanceId) {
        return createWorkOrder(facadeWorkCellInstanceMapper.getById(workInstanceId));
    }

    /**
     * 构造工作内容步骤表单信息
     *
     * @param cell
     * @return
     */
    private FacadeWorkCellInstance createWorkOrder(FacadeWorkCellInstance cell) {
        // 获取工作内容表单项
        List<FacadeFatherItem> ffi = facadeWorkOrderService.getItemsByCellId(cell.getWorkInstanceId(), cell.getStepVersion());
        List<FacadeFatherItem> newFfi = new ArrayList<>();
        if (ffi != null && ffi.size() > 0) {
            for (FacadeFatherItem item : ffi) {
                // 获取表单项对应的数据项信息
                item.setDataItems(facadeWorkOrderService.getDataItemsByFatherId(item.getFatherItemId(), null));
                newFfi.add(item);
            }
            // 工作内容赋值操作单元表单
            cell.setItems(newFfi);
        }
        // 获取工作内容步骤信息
        List<FacadeStep> fc = facadeWorkOrderService.getStepsByCellId(cell.getWorkInstanceId(), cell.getStepVersion(), null);
        List<FacadeStep> newFc = new ArrayList<>();
        if (fc != null && fc.size() > 0) {
            for (FacadeStep step : fc) {
                // 步骤对应的数据项信息
                step.setDataItems(facadeWorkOrderService.getDataItemsByStepId(step.getStepId(), step.getStepResultId()));
                // 步骤对应的图片信息
                step.setStepFiles(fileService.findByBusiness(step.getStepId()));
                newFc.add(step);
            }
            // 工作内容赋值步骤
            cell.setSteps(newFc);
        }
        return cell;
    }
}
